#include "qdrivevaluemap.h"

#include <QVariant>

QHeaderInfo::QHeaderInfo(const QString &text)
{
    setProperty("headerText",text);
    setProperty("isEdited",true);
    setProperty("type","lineEdit");
}

QDriveValueMap::QDriveValueMap()
{
}

void QDriveValueMap::addHeader(QHeaderInfo *info)
{
    m_headList.removeAll(info);
    m_headList.append(info);
    for(int i=0;i<m_valueMap.size();i++)
    {
        QStringList list=m_valueMap.at(i);
        list.append(info->property("default_value").toString());
        m_valueMap.replace(i,list);
    }
}

void QDriveValueMap::insertHeader(int index, QHeaderInfo *info)
{
    m_headList.removeAll(info);
    m_headList.insert(index,info);
    for(int i=0;i<m_valueMap.size();i++)
    {
        QStringList list=m_valueMap.at(i);
        list.insert(index,info->property("default_value").toString());
        m_valueMap.replace(i,list);
    }
}

void QDriveValueMap::removeHeader(const QString &text)
{
    foreach(QHeaderInfo *info,m_headList)
    {
        if(info->property("headerText").toString()==text)
        {
            m_headList.removeAll(info);
            delete info;
            return;
        }
    }
}

void QDriveValueMap::addRow()
{
    QStringList list;
    foreach(QHeaderInfo *info,m_headList)
    {
        list.append(info->property("default_value").toString());
    }
    m_valueMap.append(list);
    emit changed();
}

void QDriveValueMap::removeRow(int row)
{
    if(row<0 || row>=m_valueMap.size())
    {
        return;
    }
    m_valueMap.removeAt(row);
    emit changed();
}

void QDriveValueMap::setValue(int row, int column, const QString &value)
{
    if(row<0 || row>=m_valueMap.size() || column<0 || column>=m_headList.size())
    {
        return;
    }
    QStringList list=m_valueMap.at(row);
    if(list.at(column)!=value)
    {
        list.replace(column,value);
        m_valueMap.replace(row,list);
        emit valueChanged(row,column,value);
        emit changed();
    }
}

QHeaderInfo *QDriveValueMap::header(int column)
{
    if(column<0 || column>=m_headList.size())
    {
        return NULL;
    }
    return m_headList.at(column);
}

QString QDriveValueMap::value(int row, int column)
{
    if(row<0 || row>=m_valueMap.size() || column<0 || column>=m_headList.size())
    {
        return "";
    }
    return m_valueMap.at(row).at(column);
}

QList<QHeaderInfo*> QDriveValueMap::headers()
{
    return m_headList;
}

QList<QStringList> QDriveValueMap::values()
{
    return m_valueMap;
}

int QDriveValueMap::rowCount()
{
    return m_valueMap.size();
}

int QDriveValueMap::columnCount()
{
    return m_headList.size();
}

int QDriveValueMap::column(const QString &text)
{
    for(int i=0;i<m_headList.size();i++)
    {
        if(m_headList.at(i)->property("headerText").toString()==text)
        {
            return i;
        }
    }
    return -1;
}
